package linkedlist;

import java.util.Iterator;
import java.util.function.Consumer;

public class SinglyLinkedListSentinel implements Iterable<Integer> {
    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            Node pointer = head.next;

            @Override
            public boolean hasNext() {
                return pointer != null;
            }

            @Override
            public Integer next() {
                int value = pointer.value;
                pointer = pointer.next;
                return value;
            }
        };
    }

    private static class Node {
        int value;
        Node next;

        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }
    }

    // 头指针为sentinel哨兵
    private Node head = new Node(567, null);

    public void addFirst(int value) {
        insert(0, value);
    }

    /**
     * 通过索引插入值
     *
     * @param index 目标索引
     * @param value 目标值
     */
    public void insert(int index, int value) {
        Node prev = findNode(index - 1);
        throwIllegalIndex(index, prev);
        prev.next = new Node(value, prev.next);
    }

    private static void throwIllegalIndex(int index, Node prev) {
        if (prev == null) {
            throw new IllegalArgumentException(String.format("您给的索引[%d]不合法！", index));
        }
    }

    /**
     * 通过索引查找节点
     *
     * @param index 目标索引
     * @return 返回节点，如果没找到返回空
     */
    private Node findNode(int index) {
        int i = -1;
        for (Node pointer = head; pointer != null; pointer = pointer.next, i++) {
            if (i == index) {
                return pointer;
            }
        }
        return null;
    }

    /**
     * 通过索引获取node值
     *
     * @param index 目标索引
     * @return 返回node值
     */
    public int get(int index) {
        if (index == -1) {
            throwIllegalIndex(-1, null);
        }
        Node node = findNode(index);
        if (node == null) {
            throwIllegalIndex(index, node);
        }
        return node.value;
    }

    /**
     * 通过for循环遍历获取链表值
     *
     * @param consumer 函数式接口
     */
    public void loop1(Consumer<Integer> consumer) {
        for (Node pointer = head.next; pointer != null; pointer = pointer.next) {
            consumer.accept(pointer.value);
        }
    }

    /**
     * while循环遍历获取链表值
     *
     * @param consumer 函数式接口
     */
    public void loop2(Consumer<Integer> consumer) {
        Node pointer = head.next;
        while (pointer != null) {
            consumer.accept(pointer.value);
            pointer = pointer.next;
        }
    }

    public void loop3() {
        recursion(head);
    }

    /**
     * 递归
     *
     * @param node node节点
     */
    public void recursion(Node node) {
        if (node == null) {
            return;
        }
        System.out.println(node.value);
        recursion(node.next);
    }

    /**
     * 删除第一个节点
     */
    public void removeFirst() {
        remove(0);
    }

    /**
     * 通过索引删除链表节点
     *
     * @param index 目标索引
     */
    public void remove(int index) {
        Node prev = findNode(index - 1);
        throwIllegalIndex(index, prev);
        throwIllegalIndex(index, prev.next);
        prev.next = prev.next.next;
    }
}
